package br.com.furb.resource.scannerconstants;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Efetua a leitura das constantes utilizadas pelo reconhecedor.
 *  
 * @since 12/10/2011
 */
public final class FileConstantsReader {
	
	private static final String PATH = "br/com/furb/resource/scannerconstants/";
	
	private static final String SCANNER_TABLE = "scannerTable";
	private static final String SCANNER_TABLE_INDEXES = "scannerTableIndexes";
	private static final String TOKEN_STATE = "tokenState";
	private static final String SCANNER_ERROR = "scannerError";

	/**
	 * @return int[]
	 */
	public static int[] readScannerTableIndexes(){
		BufferedReader buff = openFile(SCANNER_TABLE_INDEXES);
		List<Integer> indexes = new ArrayList<Integer>();
		
		String line = "";
		
		try {
			while((line = buff.readLine()) != null){
				indexes.add(Integer.valueOf(line));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return asArray(indexes);
	}
	
	/**
	 * @return int[][]
	 */
	public static int[][] readScannerTable(){
		
		BufferedReader buff = openFile(SCANNER_TABLE);
		List<Tupla<Integer, Integer>> indexes = new ArrayList<Tupla<Integer, Integer>>();
		
		String line = "";
		
		try {
			while((line = buff.readLine()) != null){
				String[] values = line.split(",");
				indexes.add(new Tupla<Integer, Integer>(Integer.valueOf(values[0]), Integer.valueOf(values[1])));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return as2DArray(indexes);
	}
	
	/**
	 * @return int[]
	 */
	public static int[] readTokenState(){
		BufferedReader buff = openFile(TOKEN_STATE);
		List<Integer> indexes = new ArrayList<Integer>();
		
		String line = "";
		
		try {
			while((line = buff.readLine()) != null){
				indexes.add(Integer.valueOf(line));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return asArray(indexes);
	}
	
	/**
	 * @return string[]
	 */
	public static String[] readScannerError(){
		BufferedReader buff = openFile(SCANNER_ERROR);
		List<String> indexes = new ArrayList<String>();
		
		String line = "";
		
		try {
			while((line = buff.readLine()) != null){
				indexes.add(line.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return asStringArray(indexes);
	}
	
	/**
	 * @param fileName
	 * @return bufferedReader
	 */
	private static BufferedReader openFile(String fileName){
		try {
			URL url = ClassLoader.getSystemResource(PATH + fileName);
			return new BufferedReader(new FileReader(url.getFile()));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @param indexes
	 * @return int[]
	 */
	private static int[] asArray(List<Integer> indexes){
		int[] ind = new int[indexes.size()];
		
		for(int i=0;i<indexes.size();i++)
			ind[i] = indexes.get(i);
		
		return ind;
	}
	
	/**
	 * @param indexes
	 * @return string[]
	 */
	private static String[] asStringArray(List<String> indexes){
		String[] ind = new String[indexes.size()];
		
		for(int i=0;i<indexes.size();i++)
			ind[i] = indexes.get(i);
		
		return ind;
	}
	
	/**
	 * @param indexes
	 * @return int[][]
	 */
	private static int[][] as2DArray(List<Tupla<Integer,Integer>> indexes){
		int[][] ind = new int[indexes.size()][];
		
		for(int i=0;i<indexes.size();i++){
			ind[i] = new int[2]; 
			ind[i][0] = indexes.get(i).getValue1();
			ind[i][1] = indexes.get(i).getValue2();
		}
		return ind;
	}
}
